"""
|===== Conference Bot Plugin ============
|= Information Commands
|===== By: ==============================
|= Patrick Kennedy
|===== Current Version: =================
|= 1.0
|===== Description: =========================================
|= Various commands designed to display information about the bot.
|===== Additional Comments: =================================
|= 
|============================================================"""

from ini import core
from utils import *

def cmd_help(bot,user,args):
	"""Display this help message.
	Usage: )help [command]
	It will display more detailed information about a command listed.
	
	Also the different brackets have different meanings:
	* [] - Anything enclosed in these is optional.
	Ex: The usage above works with or without a command name.
	* <> - Anythign enclosed in these is required.
	Usually these will come with a list and/or explaination in the help."""
	
	if args:
		args = args.split(" ",1)[0]
		if args in bot.commands:
			bot.msg(user, bot.commands[args].__doc__)
		elif args in bot.modcommands:
			bot.msg(user, bot.modcommands[args].__doc__)
		elif args in bot.admincommands:
			bot.msg(user, bot.admincommands[args].__doc__)
		else:
			bot.error(user, "There is no command %s. Please check your spelling." % args)
	else:
		reply = """
		Commands must be prefixed by one of these symbols: %s
		For more information about a command type )help <command name>.
		To more learn about command "usage"s type )help help
		""" % (bot.config.system.commandprefix)
		if ismuted(user):
			for i in sortdict(bot.mutecommands):
				try:
					reply = reply + "%s | %s\n" % (i, bot.mutecommands[i].__doc__.split("\n")[0])
				except:
					continue
		else:
			for i in sortdict(bot.commands):
				try:
					reply = reply + "%s | %s\n" % (i, bot.commands[i].__doc__.split("\n")[0])
				except:
					continue
			if ismod(user) or isadmin(user):
				reply = reply + "\nModerator commands:\n"
				for i in sortdict(bot.modcommands):
					try:
						reply = reply + "%s | %s\n" % (i, bot.modcommands[i].__doc__.split("\n")[0])
					except:
						continue
			if isadmin(user):
				reply = reply + "\nAdmin commands:\n"
				for i in sortdict(bot.admincommands):
					try:
						reply = reply + "%s | %s\n" % (i, bot.admincommands[i].__doc__.split("\n")[0])
					except:
						continue
		bot.msg(user,reply)

def cmd_names(bot, user, args):
	"""List all the people in the room.
	Key:
	'@' - Admin
	'%' - Mod
	'-' - Away
	'!' - Busy
	'#' - Banned"""
	
	names = []
	admins = []
	online = []
	offline = []
	away = []
	idle = []
	busy = []
	
	for i in bot.getRoster():
		name = getdisplayname(i)
		if name == core.required.username:
			continue
		jidStatus = bot.getJidStatus(i).items()
		if jidStatus != []:
			for who,(status,display) in jidStatus:
				if '@' not in unicode(who):
					continue
				if has_userflag(who, "status", 'banned'):
						name = "#%s" % name
						continue
						
				if [(unicode(jid),message) for (jid,(status,message)) in jidStatus if status in ["online","chat"]]:
					if isadmin(who):
						name = "@%s" % name
						admins.append(name)
					elif ismod(who):
						name = "%"+"%s" % name
						admins.append(name)
					if has_userflag(who, "status", 'away'):
						name = "-%s" % name
						away.append(name)
					else:
						online.append(name)
					break	
				
				#Anyone not "available".
				elif [(unicode(jid),message) for (jid,(status,message)) in jidStatus if status in [u"away",u"dnd",u"xa"]]:
					if status in [u"away",u"xa"]:
						name = "-%s" % name
						idle.append(name)
					elif status == u"dnd":
						name = "!%s" % name
						busy.append(name)
					break
		else:
			offline.append('(%s)' % name)
			
	string = getformat(user,"names",'Users:\n\nIdle: (%(d_idle)d)\n%(idle)s\n\nAway: (%(d_away)d)\n%(away)s\n\nBusy: (%(d_busy)d)\n%(busy)s\n\nAvailable: (%(d_on)d)\n%(on)s\n\nTotal: (%(d_total)d)')
			
	bot.sys(user, string % {
		"d_off":len(offline), 
		"off":" ".join(offline), 
		"d_idle":len(idle), 
		"idle":" ".join(idle),
		"d_away":len(away),  
		"away":" ".join(away), 
		"d_busy":len(busy), 
		"busy":" ".join(busy), 
		"d_on":len(online), 
		"on":" ".join(online), 
		"d_total":len(online + offline + idle + away + busy), 
		"total":(online + busy + idle + away + offline)
	})

def cmd_whois(bot, user, args):
	"""Displays a user's info.
	Usage: )whois <username>"""
	if not args:
		bot.msg(user,cmd_whois.__doc__)
		return
		
	i = getjid(args.lower())
	try:
		for who,(status,display) in bot.getJidStatus(i).items():
			bot.msg(user, "Who:\n%s\n\n Status:\n%s\n\n Display:\n%s\n----------" % (who, status, display))
	except:
		bot.msg(user,"That user doesn't exist.")

def init(bot):
	bot.addCommand("help", cmd_help)
	bot.addMuteCommand("help", cmd_help)
	bot.addCommand("names",cmd_names)
	bot.addCommand("w",cmd_names)
	bot.addCommand("whois",cmd_whois)
